interface Connection {
  connect: () => void;
  disconnect: () => void;
  on: (event: string, callback: Function) => void;
}
export function createConnection(serverUrl: string, roomId: string): Connection {
  let connectedCallback: Function;
  let timeout: NodeJS.Timeout;

  return {
    connect() {
      timeout = setTimeout(() => {
        if (connectedCallback) {
          connectedCallback();
        }
      }, 100);
    },
    on(event: string, callback: Function) {
      if (connectedCallback) {
        throw Error("不能触发两次");
      }
      if (event !== "connected") {
        throw Error("只能支持connected事件");
      }
      connectedCallback = callback;
    },
    disconnect() {
      clearTimeout(timeout);
    }
  };
}
